package march.firstmarch21.march02;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class CallReducer extends Reducer<Text,CallBean, NullWritable,CallBean> {
    @Override
    protected void reduce(Text key, Iterable<CallBean> values, Context context) throws IOException, InterruptedException {

        //定义所需变量
        CallBean callBean = new CallBean();
        String phone;
        Long callLen = 0L;
        Long acceptLen = 0L;
        int callTime = 0;
        int acceptTime = 0;

        //遍历并进行判断对打电话和接电话分开进行累加时长和次数
        for (CallBean value : values) {
            callBean.setPhone(value.getPhone());
            if(value.getFlag().equals("call")){
                callLen = Long.parseLong(value.getCallLen());
                callTime ++;
            }else{
                acceptLen = Long.parseLong(value.getAcceptLen());
                acceptTime++;
            }
        }

        //填充对象
        callBean.setCallLen(String.valueOf(callLen));
        callBean.setCallTime(String.valueOf(callTime));
        callBean.setAcceptLen(String.valueOf(acceptLen));
        callBean.setAcceptTime(String.valueOf(acceptTime));

        context.write(NullWritable.get(),callBean);
    }
}
